<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class editgroups {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('personnel');
    $masterclass->loadjavascript('',$masterclass->mainconfig['moduledirectory'].'admin/editgroups.js');
    
    $masterclass->loadjavascript('',$masterclass->mainconfig['v4rootdir'].'include/suckertreemenu.inc.js');

    $masterclass->site['postfields'][] = array(
      'table'=>'trackgroups',
      'trackingid'=>'trackgroupsid',
      'fields'=>array(
        'trackgroups_name'=>'name'
      )
    );
    if ((!isset($masterclass->sitevars['trackgroupsid']) || $masterclass->sitevars['trackgroupsid']=='')) {
      $masterclass->site['postfields'][] = array(
        'table'=>'trackgroups',
        'trackingid'=>'trackgroupsid',
        'fields'=>array(
          'trackgroups_type'=>'uniquelist',
        )
      );
    }
  return;
  }

  function savedata() {
    global $masterclass;
    

    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
       case 'editgroups_save':
       
        if ($masterclass->sitevars['trackgroupsid']<1 || $masterclass->sitevars['trackgroupsid']=='') {
          $newgroupflag='1';
        }
        $masterclass->savedata();
        
        if ($newgroupflag=='1') {
          $masterclass->site['forward']=$masterclass->mainconfig['dir'].'admin/editgroups.php?'.$masterclass->trackingvar_query();
        } else {
          $masterclass->site['forward']='window.close();';
        }
       break;
       case 'editgroups_newgroup':
        
        $masterclass->savedata();
        $masterclass->site['forward']=$masterclass->mainconfig['dir'].'admin/editgroups.php';
       break;
      }
    }
    
  return;
  }

  function swap_trackgroupsmember() {
    global $masterclass;

    $containername=$_POST['rsargs'][1];
    $groupid=$_POST['rsargs'][2];
    $groupmemberid=$_POST['rsargs'][3];

    $groupmembers=$masterclass->fetch_group_members($groupid);
    $new_groupmembers='';

    if (!in_array($groupmemberid,$groupmembers)) {
      //if (!empty($groupmembers)) { $new_groupmembers=','.implode(',',$groupmembers); }
      foreach ($groupmembers as $member) {
        if ($member!='') { $new_groupmembers.=','.$member; }
      }
      $new_groupmembers.=','.$groupmemberid;
      $divclass='sitebox_trackgroups_member';
    } else {
      foreach ($groupmembers as $member) {
       if ($member!=$groupmemberid) { $new_groupmembers.=','.$member; }
      }
      $divclass='sitebox_content';
    }

    $dbinfo = array(
      'table'=>'trackgroups',
      'trackingid'=>'trackgroupsid',
      'static'=>array(
        $new_groupmembers.','=>'members',
      )
    );
    $masterclass->uploaddata($dbinfo);

    $masterclass->site['grabdata_vars']['containername']=$containername;
    $masterclass->site['grabdata_vars']['divclass']=$divclass;

  }

  function display_groupmemberslist($containername='',$listview='',$pg=1,$grouptype='',$groupid='') {
    global $masterclass;
    $grouptable='';
    $displayfields=array();
    $orderbyfields=array();

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $listview=$_POST['rsargs'][2];
      $pg=$_POST['rsargs'][3];
      $grouptype=$_POST['rsargs'][4];
      $groupid=$_POST['rsargs'][5];
    }

    if ($pg<0) { $pg=1; }

    if ($grouptype=='' && $groupid=='') { return; }

    $groupmembers=$masterclass->fetch_group_members($groupid);

    switch ($grouptype) {
     case 'personnelid':
       $grouptable='personnel';
       $displayfields=array('first','last');
       $orderbyfields=array('`first`','`last`');
     break;
    }

    switch ($listview) {
     default:
      $headline='Listing All';
     break;
     case 'members':
      $strgroupwhere='';
      if (!empty($groupmembers)) {
        foreach ($groupmembers as $groupmember) {
          if ($strgroupwhere!='') { $strgroupwhere.=' OR '; }
          $strgroupwhere.=sprintf(" (`id`=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($groupmember));
        }
        if ($strgroupwhere!='') { if ($strwhere!='') { $strwhere.=' OR '; } $strwhere.=' ('.$strgroupwhere.') '; }
      }
      $headline='Showing Only Group Members';
     break;
     case 'nonmembers':
      $strgroupwhere='';
      if (!empty($groupmembers)) {
        foreach ($groupmembers as $groupmember) {
          if ($strgroupwhere!='') { $strgroupwhere.=' AND '; }
          $strgroupwhere.=sprintf(" (`id`!=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($groupmember));
        }
        if ($strgroupwhere!='') { if ($strwhere!='') { $strwhere.=' OR '; } $strwhere.=' ('.$strgroupwhere.') '; }
      }
      $headline='Showing Only NON Group Members';
     break;
    }

    $strorderby='';
    if (!empty($orderbyfields)) { foreach ($orderbyfields as $orderbyfield) { if ($strorderby!='') { $strorderby.=','; } $strorderby.=$orderbyfield; } }
    $perpage=50;
    $listcnt=$masterclass->fetch_recordcount($grouptable,$strwhere);
    $listpgcount=ceil($listcnt/$perpage);
    $listarray=$masterclass->fetch_records($grouptable,$pg,$strwhere,' '.$strorderby.' ',$perpage);

    $ret='';
    $ret.='<div class="sitebox_navigation" style="text-align:center;font-weight:800;border-bottom:1px solid;"><b>'.$headline.'</b></div>';
    $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
    $ret.='<tr class="sitebox_navigation">';
        foreach ($displayfields as $displayfield) {
          $ret.='<td valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr><b>'.$displayfield.'</b></nobr>&nbsp;</td>';
        }
    $ret.='</tr>';
    if (empty($listarray)) {
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Records To List</td>';
      $ret.='</tr>';
    } else {
      foreach ($listarray as $cl) {
        if (in_array($cl['id'],$groupmembers)) { $divclass='sitebox_trackgroups_member'; } else { $divclass='sitebox_content'; }
        $ret.='<tr valign="top" id="trackgroups_container_'.$cl['id'].'" class="'.$divclass.'" OnClick="swap_groupmember(\'swap_trackgroupsmember\',\'trackgroups_container_'.$cl['id'].'\',\''.$masterclass->sitevars['trackgroupsid'].'\',\''.$cl['id'].'\');this.style.cursor=\'wait\';" style="cursor:pointer;">';
        foreach ($displayfields as $displayfield) {
          $ret.='<td valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr>'.$cl[$displayfield].'</nobr>&nbsp;</td>';
        }
        $ret.='</tr>';
      }
    }
    $ret.='</table>';

    $listdisplay=$ret;

    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_listpage']=$pg;
    $masterclass->site['grabdata_vars']['load_listpagecnt']=$listpgcount;
    $masterclass->site['grabdata_vars']['load_listcnt']=$listcnt;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $ret;
  }
  function display() {
    global $masterclass;
    if (isset($masterclass->sitevars['trackgroupsid'])) { $grouptype=$masterclass->fetch_grouptype($masterclass->sitevars['trackgroupsid']); }
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top">
    <td>
<?php
  $pageheader='<table width="100%" cellpadding="0" cellspacing="0" border="0" class="sitebox_pageheader"><tr><td style="text-align:left;">Groups Editor</td><td style="text-align:right;" width="25"><div style="font-size:8pt;"><nobr>'.$masterclass->allclasses['personnel']->fetch_personnelname($masterclass->site['personnelid']).' ['.$masterclass->site['personnelid'].']</nobr></div><div style="font-size:8pt;"><nobr>'.$_SERVER['REMOTE_ADDR'].'</nobr></div></td></tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_pageheader($pageheader,'');
?>
  </td>
  </tr>
</table>
<?php
$nav='';
$nav.='<div class="sitebox_navigation" style="width:100%;">';
$nav.='<ul id="sitebox_navmenu">';
$nav.='<li OnClick="jsshowloadingscreen();add_post_action(\'editgroups_save\');frm.submit();"><a>Save</a></li>';
$nav.='<li OnClick="jsshowloadingscreen();add_post_action(\'editgroups_save\');add_post_action(\'editgroups_newgroup\');frm.submit();"><a>New Group</a></li>';
$nav.='<li OnClick="window.close();"><a>Close</a></li>';
$nav.='</ul>';
$nav.='<br style="clear: left;" />';
$nav.='</div>';
echo $nav;
?>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
  <tr valign="top"><td colspan="2">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Select Group','');
?>
   <table width="100%" cellspacing="0" cellpadding="2" border="0" class="sitebox_content">
      <tr><td><input type="button" class="button" value=" Edit Group " OnClick="window.location='<?php echo $masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'];?>editgroups.php?trackgroupsid='+document.getElementById('trackgroups_changegroup').options[document.getElementById('trackgroups_changegroup').selectedIndex].value;">
&nbsp;&nbsp;<?php
$options=array();
$options[]=array('');
$arrgrouplist=$masterclass->fetch_groups();
if (!empty($arrgrouplist)) {
  foreach ($arrgrouplist as $groupitem) {
    $options[]=array($groupitem['id'],$groupitem['name']);
  }
}
  echo $masterclass->allclasses['websites']->disp_select($options,'trackgroups_changegroup',$masterclass->sitevars['trackgroupsid'],'75%');
?>&nbsp;&nbsp;</td></tr>
    </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
  </td></tr>
  <tr valign="top">
  <td width="35%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Group Information','');
?>
   <table cellspacing="0" cellpadding="2" border="0" class="sitebox_content">
      <tr><td>Group Name:</td><td><?php echo $masterclass->allclasses['websites']->disp_inputtext('trackgroups_name');?></td></tr>
      <tr><td>Group Type:</td><td><?php
if ((!isset($masterclass->sitevars['trackgroupsid']) || $masterclass->sitevars['trackgroupsid']=='')) {
$options=array();
$options[]=array('personnelid');
  echo $masterclass->allclasses['websites']->disp_select($options,'trackgroups_type');
} else {
  echo $masterclass->fetch_grouptype($masterclass->sitevars['trackgroupsid']);
}?></td></tr>
    </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
echo $masterclass->allclasses['websites']->sitebox_vspacer();

?>
  </td><td width="65%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Group Members','');

  $groupmemberslist_click='update_groupmemberslist();';

  $unquieid='groupmemberslist_container';
  $runcmdpre='';
  $cngpg_cmd="display_groupmembers_list('display_groupmemberslist','$unquieid',listview,document.getElementById('pagelist$unquieid').selectedIndex+1,'".$grouptype."','".$masterclass->sitevars['trackgroupsid']."');";
  $runcmdpost=$cngpg_cmd;

  $nav='';
  $nav.='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr valign="middle" class="sitebox_navigation">';
  $nav.='<td style="text-align:left;">';
  $nav.='<input type="button" class="button" value="Non-Members" OnClick="listview=\'nonmembers\';'.$groupmemberslist_click.'">&nbsp;&nbsp;';
  $nav.='<input type="button" class="button" value="Members" OnClick="listview=\'members\';'.$groupmemberslist_click.'">&nbsp;&nbsp;';
  $nav.='<input type="button" class="button" value="All" OnClick="listview=\'\';'.$groupmemberslist_click.'">&nbsp;&nbsp;';
  $nav.='<input type="button" class="button" value="Refresh" OnClick="'.$groupmemberslist_click.'">&nbsp;&nbsp;';
  $nav.='</td>';
  $nav.='</tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_navigation($nav);

  echo $masterclass->allclasses['websites']->disp_pages($runcmdpre,$runcmdpost,$unquieid);
?>
<div id="<?php echo $unquieid; ?>"><?php echo $this->display_groupmemberslist($unquieid,'',1,$grouptype,$masterclass->sitevars['trackgroupsid']); ?></div>
<script language="javascript">
  var listview='';
  function update_groupmemberslist() {
    <?php echo $cngpg_cmd; ?>
  }
</script>
<?php

  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
</td></tr></table>
<?php

  return;
  }
}
?>